package com.example.QiLiuYunService;

import com.example.Utility.Result;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 财哥
 * <p>
 * 2023.4.4
 * Mybitas-puls
 */
@RestController
@RequestMapping("serviceImage")
public class serviceImage {

    @Value("${qiniuyun.accessKey}")
    String accessKey;
    @Value("${qiniuyun.secretKey}")
    String secretKey;
    @Value("${qiniuyun.bucket}")
    String bucket;

    @Value("${Image_path.path}")
    private String this_path;


    @PostMapping("upload")
    public Result upload(MultipartFile file) {
//构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
//...其他参数参考类注释
        String Original = file.getOriginalFilename();
        //         存入文件名
        String ServiceNames = UUID.randomUUID() + Original.substring(Original.lastIndexOf("."));
        //创建本都文件对象
        File file1;
        try {
            file1 = new File(this_path);
            if (file1.exists()) {
                file1.mkdirs();
            }
            file.transferTo(new File(file1 + ServiceNames));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        UploadManager uploadManager = new UploadManager(cfg);
        String localFilePath = file1 + ServiceNames;
//默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = ServiceNames;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
/**
 * 获取外链
 */
            //自己的域名路径+文件名
            String publicUrl = "https://caige.site/" + ServiceNames;
            long expireInSeconds = 360000000; //1小时，可以自定义链接过期时间
            String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
            return Result.succeed(finalUrl);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return Result.fail("失败");
    }

}
